#include <string>
#include <iostream>
#include <sstream>//ostringstream将各种类型的数据转换为字符串
using namespace std;

class Time
{
    int hour=0 ;
    int minute =0;
    int second=0 ;//防止潜在问题
public:
    Time();//构造函数用于初始化
    Time(int h, int m, int s);//构造函数的参数个数或者类型必须不同
    //确保传入的值合法有效
    //getter and setter
    int getHour();
    int getMinute();
    int getSecond();
    void setHour(int h);
    void setMinute(int m);
    void setSecond(int s);//int,void类型
    //display
    string toString24();
    string toString12();
};
Time::Time()
{
    hour = minute = second = 0;
}
Time::Time(int h, int m, int s)
{
    if(h >= 0 && h < 24)
        hour = h;
    if(m >= 0 && m < 60)
        minute = m;
    if(s >= 0 && s < 60)
        second = s;
}
//虽然在 main 函数中没有直接使用get函数，
//但它们提供了一个访问成员变量的接口，符合封装的原则？？？？？
int Time::getHour()
{
    return hour;
}
int Time::getMinute()
{
    return minute;
}
int Time::getSecond()
{
    return second;
}
//确保传入值有效
void Time::setHour(int h)
{
    if(h >= 0 && h < 24)
        hour = h;
}
void Time::setMinute(int m)
{
    if(m >= 0 && m < 60)
        minute = m;
}
void Time::setSecond(int s)
{
    if(s >= 0 && s < 60)
        second = s;
}

string Time::toString24()
{
    ostringstream os;
    os << hour << ":" << minute << ":" << second;
    return os.str();
}

string Time::toString12()
{
    ostringstream os;

    if(hour > 0 && hour < 12)
        os << hour << ":" << minute << ":" << second << " A.M.";
    else if(hour == 0)
        os << (hour + 12) << ":" << minute << ":" << second << " A.M.";
    else if(hour == 12)
        os << hour << ":" << minute << ":" << second << " P.M.";
    else
        os << (hour - 12) << ":" << minute << ":" << second << " P.M.";

    return os.str();
}

int main()
{
    Time t1(23, 15, 35);
    Time t2(12, 35, 7);
    Time t3(0, 23, 19);
    Time t4;
//没用到get函数呀？
    cout << t1.toString12() << "\t" << t1.toString24() << endl;
    cout << t2.toString12() << "\t" << t2.toString24() << endl;
    cout << t3.toString12() << "\t" << t3.toString24() << endl;
    cout << t4.toString12() << "\t" << t4.toString24() << endl;

    return 0;
}
/*
1.什么叫做main函数中没有直接使用get函数但是符合封装的原则？
封装的原则是什么？
封装的目的是什么？
封装的好处是什么？
封装的实现方法是什么？
2.我觉得set函数和第二个构造函数功能很重复，都是为了确保输入合法
*/
/*  t4.setHour(10);
    t4.setMinute(45);
    t4.setSecond(30);
     cout<<"t4: "<< t4.getHour() 
     cout<< ":" << t4.getMinute() 
  */

